package com.northpool.operator.common.algorithm;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

public abstract class Helper {
    private static final String UTF_8 = "UTF-8";

    public Helper() {
    }

    public static String getFileExt(String fn) {
        String fname = (new File(fn)).getName();
        int p = fname.lastIndexOf(".");
        return p != -1 ? fname.substring(p + 1) : "";
    }

    public static String randomStr() {
        return "A" + UUID.randomUUID().toString().replace("-", "").toUpperCase();
    }

    public static String concatFileName(String fn, String perfix, String suffix) {
        return ("".equals(perfix) ? "" : perfix + "_") + fn + ("".equalsIgnoreCase(suffix) ? "" : "_" + suffix);
    }

    public static String randomFileName(String perfix, String suffix, String ext) {
        String fn = concatFileName(randomStr(), perfix, suffix);
        if (ext != null && !"".equals(ext)) {
            if (!ext.startsWith(".")) {
                ext = "." + ext;
            }

            return fn + ext;
        } else {
            return fn;
        }
    }

    protected void ensureParent(File file) {
        ensurePath(file.getParentFile());
    }

    public static File ensurePath(String path) {
        return ensurePath(new File(path));
    }

    public static File ensurePath(File p) {
        if (!p.exists()) {
            p.mkdirs();
        }

        return p;
    }

    public static File randomFile(String path, String perfix, String suffix, String ext) {
        return new File(ensurePath(path), randomFileName(perfix, suffix, ext));
    }

    public static File randomPath(String path, String perfix, String suffix) {
        return ensurePath(new File(path, concatFileName(randomStr(), perfix, suffix)));
    }

    public static String getStr(Map<String, String> params, String key, String def) {
        String v = def;
        if (params.containsKey(key)) {
            v = (String)params.get(key);
            if (v == null || "".equals(v.trim())) {
                v = def;
            }
        }

        return v;
    }

    public static boolean getBool(Map<String, String> params, String key, boolean def) {
        return Boolean.parseBoolean(getStr(params, key, Boolean.toString(def)));
    }

    public static Map<String, String> parseXMLFile(File filePath) {
        Map<String, String> result = new LinkedHashMap();

        try {
            Throwable var2 = null;
            Object var3 = null;

            try {
                InputStream ri = new BufferedInputStream(new FileInputStream(filePath));

                try {
                    SAXReader reader = SAXReader.createDefault();
                    reader.setEncoding("UTF-8");
                    Document doc = reader.read(ri);
                    Element root = doc.getRootElement();
                    root.elements().forEach((e) -> {
                        // TODO 后面一定删除，适配
                        result.put(e.elementTextTrim("name"), e.elementTextTrim("value"));
                    });
                } finally {
                    if (ri != null) {
                        ri.close();
                    }

                }
            } catch (Throwable var15) {
                if (var2 == null) {
                    var2 = var15;
                } else if (var2 != var15) {
                    var2.addSuppressed(var15);
                }

                throw var2;
            }
        } catch (Throwable var16) {
            var16.printStackTrace();
        }

        return result;
    }

    public static Map<String, String> xmlArg2Map(String[] args) throws Exception {
        if (args.length < 2) {
            throw new Exception("参数个数必须大于2，指定算法主类和xml格式的参数文件名");
        } else {
            File param = new File(args[1]);
            if (!param.exists()) {
                throw new Exception("参数文件【" + args[1] + "】不存在。");
            } else {
                Map<String, String> stringStringMap = parseXMLFile(param);
                for (String key : stringStringMap.keySet()) {
                    String value = stringStringMap.get(key);
                    // TODO 特殊处理，适配波哥参数转义问题，后面要干掉
                    if (value.contains("&apos;")) {
                        value = value.replace("&apos;", "'");
                        stringStringMap.put(key, value);
                    } else {
                        stringStringMap.put(key, value);
                    }
                }
                return stringStringMap;
            }
        }
    }

}
